package com.kawa.kawa_ad.handler;

import android.app.Activity;

import com.anythink.core.api.ATAdConst;
import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.rewardvideo.api.ATRewardVideoAd;
import com.anythink.rewardvideo.api.ATRewardVideoListener;
import com.kawa.kawa_ad.util.Utils;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class RewardHandler implements ATRewardVideoListener {

    private final ATRewardVideoAd mRewardAd;
    private EventChannel.EventSink mEventSink;
    private final Activity mActivity;
    private final BinaryMessenger mMessenger;

    public RewardHandler(Activity activity,BinaryMessenger messenger,String userId, String rewardId){
        mActivity = activity;
        mMessenger = messenger;
        mRewardAd = new ATRewardVideoAd(activity,rewardId);
        mRewardAd.setAdListener(this);
        Map<String,Object> map = new HashMap<>();
        map.put(ATAdConst.KEY.USER_ID,userId);
        mRewardAd.setLocalExtra(map);
    }

    public void loadReward(int channelId){
        setupEventSink(channelId);
        mRewardAd.load();
    }
    private void setupEventSink(int mChannelId){
        EventChannel mEventChannel = new EventChannel(mMessenger, "com.mob.adsdk/event_" + mChannelId);
        mEventChannel.setStreamHandler(new EventChannel.StreamHandler() {
            @Override
            public void onListen(Object o, EventChannel.EventSink eventSink) {
                mEventSink = eventSink;
            }

            @Override
            public void onCancel(Object o) {
                mEventSink = null;
            }
        });
    }

    public void show(int channelId,String scene){
        setupEventSink(channelId);
        if (mRewardAd.isAdReady()){
            mRewardAd.show(mActivity,scene);
        }else if (mRewardAd.checkAdStatus().isLoading()){
            Map<String,Object> map = new HashMap<>();
            map.put("name","loading");
            map.put("result","0");
            mEventSink.success(map);
            endSink();
        }else{
            Map<String,Object> map = new HashMap<>();
            map.put("name","loadFail");
            map.put("result","1");
            mEventSink.success(map);
            endSink();
            mRewardAd.load();
        }
    }

    private void endSink(){
        if (mEventSink != null) {
            mEventSink.endOfStream();
            mEventSink = null;
        }
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        Map<String,Object> map = new HashMap<>();
        map.put("name","loaded");
        map.put("result","0");
        mEventSink.success(map);
        endSink();
    }

    @Override
    public void onRewardedVideoAdFailed(AdError adError) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","fail");
        map.put("result","1");
        map.put("error",adError.getDesc());
        mEventSink.success(map);
        endSink();
    }

    @Override
    public void onRewardedVideoAdPlayStart(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","playStart");
        map.put("result","0");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onRewardedVideoAdPlayEnd(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","playEnd");
        map.put("result","0");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onRewardedVideoAdPlayFailed(AdError adError, ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","playFailed");
        map.put("result","1");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        map.put("error",adError.getDesc());
        mEventSink.success(map);
        endSink();
    }

    @Override
    public void onRewardedVideoAdClosed(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","closed");
        map.put("result","0");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
        endSink();
        mRewardAd.load();
    }

    @Override
    public void onRewardedVideoAdPlayClicked(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","click");
        map.put("result","0");
        map.put("info", Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }

    @Override
    public void onReward(ATAdInfo atAdInfo) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","reward");
        map.put("result","0");
        map.put("info",Utils.adInfoToMap(atAdInfo));
        mEventSink.success(map);
    }
}
